//
//  PasswordInputView.m
//  upnpxdemo
//
//  Created by 赵帅 on 13-9-1.
//  Copyright (c) 2013年 wiimu.inc. All rights reserved.
//

#import "PasswordInputView.h"
#import <QuartzCore/QuartzCore.h>

@implementation PasswordInputView
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

-(void)awakeFromNib
{
    if([GlobalInfo sharedInstance].deviceType == IPHONE4_TYPE)
    {
        _backView.frame = CGRectMake(0, 0, 320, 568);
        //        _insideView.frame = CGRectMake(_insideView.frame.origin.x, _insideView.frame.origin.y+88, _insideView.frame.size.width, _insideView.frame.size.height);
    }
    
    [_confirmButton setTitle:NSLocalizedString(@"CONFIRM",@"") forState:UIControlStateNormal];
    [_cancelButton setTitle:NSLocalizedString(@"CANCEL",@"") forState:UIControlStateNormal];
    _infoLabel.text = NSLocalizedString(@"INPUTWLANPASSWORD",@"");
    
    [self appearAnimation];
}

-(void)appearAnimation
{
    CABasicAnimation *forwardAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    forwardAnimation.duration = 0.2f;
    forwardAnimation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.5f :1.7f :0.6f :0.85f];
    forwardAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    forwardAnimation.toValue = [NSNumber numberWithFloat:1.0f];
    [_insideView.layer addAnimation:forwardAnimation forKey:@"popin"];
    
    CABasicAnimation *fadein = [CABasicAnimation animationWithKeyPath:@"opacity"];
    fadein.toValue = [NSNumber numberWithFloat:0.7];
    fadein.duration = 0.2;
    fadein.fillMode = kCAFillModeForwards;
    fadein.removedOnCompletion = NO;
    fadein.delegate = self;
    [_backView.layer addAnimation:fadein forKey:@"fadein"];
}

-(void)disappearAnimation
{
    CABasicAnimation *fadeout = [CABasicAnimation animationWithKeyPath:@"opacity"];
    fadeout.toValue = [NSNumber numberWithFloat:0];
    fadeout.duration = 0.2f;
    fadeout.fillMode = kCAFillModeForwards;
    fadeout.removedOnCompletion = NO;
    fadeout.delegate = self;
    [_backView.layer addAnimation:fadeout forKey:@"fadeout"];
    
    CABasicAnimation *reverseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    reverseAnimation.duration = 0.2f;
    reverseAnimation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.4f :0.15f :0.5f :-0.7f];
    reverseAnimation.fromValue = [NSNumber numberWithFloat:1.0f];
    reverseAnimation.toValue = [NSNumber numberWithFloat:0.0f];
    reverseAnimation.delegate = self;
    reverseAnimation.removedOnCompletion = NO;
    reverseAnimation.fillMode = kCAFillModeForwards;
    [_insideView.layer addAnimation:reverseAnimation forKey:@"kLPAnimationKeyPopup"];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    if(flag)
    {
        if(anim == [_backView.layer animationForKey:@"fadein"])
        {
            self.backView.alpha = 0.7;
            _passwordField.text = [[GlobalInfo sharedInstance] getWifiPassword:_ssid];
        }
        else if(anim == [_backView.layer animationForKey:@"fadeout"])
        {
            [self removeFromSuperview];
        }
    }
}

-(IBAction)touchDownOnBack:(id)sender
{
    [_passwordField resignFirstResponder];
}

-(IBAction)confirmButtonPressed:(id)sender
{
    NSString * password = _passwordField.text;
//    char * bytes = (char *)[password UTF8String];
//    password = [self str2HexStr:bytes];
    
    //保存密码
    [[GlobalInfo sharedInstance] addWifiInfo:_ssid password:password];
    
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:[GlobalInfo sharedInstance].currentBaseURL,@"baseurl",_ssid,@"ssid",_auth,@"auth",password,@"password",_channel,@"channel",_encry,@"encry", nil];
    
    if (nil != delegate
		&& [delegate respondsToSelector:@selector(ConfirmButtonPressed:)])
	{
        [delegate performSelectorOnMainThread:@selector(ConfirmButtonPressed:) withObject:dic waitUntilDone:YES];
	}
    
    [self disappearAnimation];
}

-(IBAction)cancelButtonPressed:(id)sender
{
    [self disappearAnimation];
    
}

-(NSString *)str2HexStr:(char*)str
{
    char chars[] = "0123456789ABCDEF";
    NSMutableString * sb = [NSMutableString string];
    int bit;
    for (int i = 0; i < strlen(str); i++)
    {
        bit = (str[i] & 0x0f0) >> 4;
        [sb appendFormat:@"%c",chars[bit]];
        bit = str[i] & 0x0f;
        [sb appendFormat:@"%c",chars[bit]];
    }
    return sb;
}

#pragma mark -
#pragma mark UITextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_passwordField resignFirstResponder];
    return YES;
}

@end
